package com.excellent.system.service.impl;

import com.excellent.common.core.domain.entity.SysUser;
import com.excellent.common.core.text.Convert;
import com.excellent.system.domain.SysStuTest;
import com.excellent.system.domain.SysUserGroupInterview;
import com.excellent.system.mapper.SysStuTestMapper;
import com.excellent.system.service.ISysStuTestService;
import com.excellent.system.service.ISysUserGroupService;
import com.excellent.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 成绩输入Service业务层处理
 *
 * @author YYY
 * @date 2020-12-24
 */
@Service
public class SysStuTestServiceImpl implements ISysStuTestService
{
    @Autowired
    private SysStuTestMapper sysStuTestMapper;

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysUserGroupService sysUserGroupService;


//    @Autowired
//    private SysUserGroupMapper userGroupMapper;

    /**
     * 查询成绩输入
     *
     * @param stuSno 成绩输入ID
     * @return 成绩输入
     */
    @Override
    public SysStuTest selectSysStuTestById(String stuSno)
    {
        return sysStuTestMapper.selectSysStuTestById(stuSno);
    }

    /**
     *
     *
     * @return 结果
     */
    public int selectCount()
    {
        return sysStuTestMapper.selectCount();
    }
    /**
     * 查询成绩输入列表
     *
     * @param sysStuTest 成绩输入
     * @return 成绩输入
     */
    @Override
    public List<SysStuTest> selectSysStuTestList(SysStuTest sysStuTest)
    {
        return sysStuTestMapper.selectSysStuTestList(sysStuTest);
    }

    /**
     * 查询成绩输入列表
     *
     * @param count 成绩输入
     * @return 成绩输入
     */
    @Override
    public List<SysStuTest> selectSysStuTestList(int count)
    {
        return sysStuTestMapper.selectSysStuTestListC(count);
    }
    /**
     * 查询成绩输入列表
     *
     * @param sysStuTest 成绩输入
     * @return 成绩输入集合
     */
    public List<SysStuTest> selectSysStuTestListByGroup(SysStuTest sysStuTest,long group){

        List<SysStuTest> list =selectSysStuTestList(sysStuTest);
        SysUserGroupInterview sysUserGroupInterview =new SysUserGroupInterview();
        sysUserGroupInterview.setGroupInterviewId(group);
        sysUserGroupInterview.setUserId(null);
        List<SysUserGroupInterview> list1=sysUserGroupService.selectSysUserGroupInterviewList(sysUserGroupInterview);
        List<SysStuTest> listDemo = new ArrayList<>();
        for(SysStuTest l : list){
            for(SysUserGroupInterview l1: list1){
                if (l.getStuSno().equals(l1.getUserId())) {
                    listDemo.add(l);
                }
            }
        }
        return listDemo;
    }
    /**
     * 新增成绩输入
     *
     * @param sysStuTest 成绩输入
     * @return 结果
     */
    @Override
    public int insertSysStuTest(SysStuTest sysStuTest)
    {
        return sysStuTestMapper.insertSysStuTest(sysStuTest);
    }

    /**
     * 修改成绩输入
     *
     * @param sysStuTest 成绩输入
     * @return 结果
     */
    @Override
    public int updateSysStuTest(SysStuTest sysStuTest)
    {
        if(sysStuTest.getgroupInterviewId()!=null){
            SysUserGroupInterview sysUserGroupInterview =new SysUserGroupInterview();
            SysUser sysUser  = sysUserService.selectUserByLoginName(sysStuTest.getStuSno());
            sysUserGroupInterview.setUserId(sysUser.getUserId().toString());
            sysUserGroupInterview.setGroupInterviewId(sysStuTest.getgroupInterviewId());
            // 删除用户与分组关联
            sysUserGroupService.deleteSysUserGroupInterviewById(sysUser.getUserId().toString());
            // 新增用户与分组管理
            sysUserGroupService.insertSysUserGroupInterview(sysUserGroupInterview);
        }

        return sysStuTestMapper.updateSysStuTest(sysStuTest);
    }

    /**
     * 删除成绩输入对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysStuTestByIds(String ids)
    {
        sysUserGroupService.deleteSysUserGroupInterviewByIds(ids);

        return sysStuTestMapper.deleteSysStuTestByIds(Convert.toStrArray(ids));
    }
    /**
     * 删除成绩输入对象
     *
     * @param count 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysStuTestFirst(int count)
    {
        return sysStuTestMapper.deleteSysStuTestFirst(count);
    }

    /**
     * 删除成绩输入对象
     *
     * @param count 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysStuTestSecond(int count)
    {
        return sysStuTestMapper.deleteSysStuTestSecond(count);
    }

    /**
     * 删除成绩输入信息
     *
     * @param stuSno 成绩输入ID
     * @return 结果
     */
    @Override
    public int deleteSysStuTestById(String stuSno)
    {
        sysUserGroupService.deleteSysUserGroupInterviewById(stuSno);
        return sysStuTestMapper.deleteSysStuTestById(stuSno);
    }
}
